import {parse} from './parseDate';

export function dateFormat (format, date) {
    format = format || 'yyyy-MM-dd';
    const o = {   
        'M+' : date.getMonth() + 1,                 // 月份   
        'd+' : date.getDate(),                    // 日   
        'h+' : date.getHours(),                   // 小时   
        'm+' : date.getMinutes(),                 // 分   
        's+' : date.getSeconds(),                 // 秒   
        'q+' : Math.floor((date.getMonth() + 3) / 3), // 季度   
        'S'  : date.getMilliseconds()             // 毫秒   
    };   
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length));
    }
    for (const k in o) {
        if (new RegExp(`(${k})`).test(format)) {
            format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)));
        } 
    }
    return format;
}

export const parseDate = parse;

export function alignPostion (align, handler, dropWrap) {
    const rect2 = dropWrap.getBoundingClientRect();
    const rect = handler.getBoundingClientRect();
    const popperStyle = {
        position: 'absolute'
    };
    if (align === 'bottom') {
        popperStyle.top = rect.height;
        popperStyle.left = rect.width / 2 - rect2.width / 2 ;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'bottomLeft') {
        popperStyle.top = rect.height;
        popperStyle.left = 0;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'bottomRight') {
        popperStyle.top = rect.height;
        popperStyle.right = 0;
        popperStyle.willChange = 'top, right';
    }
    if (align === 'top') {
        popperStyle.bottom = rect.height;
        popperStyle.left = rect.width / 2 - rect2.width / 2 ;
    }
    if (align === 'topLeft') {
        popperStyle.bottom = rect.height;
        popperStyle.left = 0;
        popperStyle.willChange = 'bottom, left';
    }
    if (align === 'topRight') {
        popperStyle.bottom = rect.height;
        popperStyle.right = 0;
        popperStyle.willChange = 'bottom, right';
    }
    if (align === 'left') {
        popperStyle.top = rect.height / 2 - rect2.height / 2;
        popperStyle.left = -rect2.width;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'leftTop') {
        popperStyle.top = 0;
        popperStyle.left = -rect2.width;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'leftBottom') {
        popperStyle.bottom = 0;
        popperStyle.left = -rect2.width;
        popperStyle.willChange = 'bottom, left';
    }
    if (align === 'right') {
        popperStyle.top = rect.height / 2 - rect2.height / 2;
        popperStyle.left = rect.width;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'rightTop') {
        popperStyle.top = 0;
        popperStyle.left = rect.width;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'rightBottom') {
        popperStyle.bottom = 0;
        popperStyle.left = rect.width;
        popperStyle.willChange = 'bottom, left';
    }
    return popperStyle;
}
